<?php

class Services extends App_Db_Model 
{
	public $id;
	public $ptype;
	public $text;
	public $title;
    protected $_name = 'services';
    
	function __construct($fieldArr = null) {
		parent::__construct();
		if (is_null($fieldArr)) {
			$this->id = 0;
			$this->ptype = 0;
			$this->text = 0;
			$this->title = "";
		} else {
			$this->setDataFromDBArray($fieldArr);
		}
	}
    
	public function getArrayFromData($withkey=true) {
		$fieldArr = array(
			"ptype" => $this->ptype,
			"text" => $this->text,
		);
		if ($withkey) $fieldArr["id"] = $this->id;
		return $fieldArr;
	}
	
	public function setDataFromArray($fieldArr) {
		global $serviceTypes;
		$this->id = $fieldArr["id"];
		$this->ptype = $fieldArr["ptype"];
		$this->text = stripslashes($fieldArr["text"]);
		$this->title = $serviceTypes[$this->id];
	}

	public function getDBArrayFromData($withkey=true) {
		$fieldArr = array(
			"ptype" => $this->ptype,
			"text" => $this->text,
		);
		if ($withkey) $fieldArr["id"] = $this->id;
		$fieldArr = iconvObj($fieldArr, UTF_CHARSET, CP1251_CHARSET);
		return $fieldArr;
	}
	
	public function setDataFromDBArray($fieldArr) {
		global $serviceTypes;
		$fieldArr = iconvObj($fieldArr, CP1251_CHARSET, UTF_CHARSET);
		$this->id = $fieldArr["id"];
		$this->ptype = $fieldArr["ptype"];
		$this->text = $fieldArr["text"];
		$this->title = $serviceTypes[$this->id];
	}
	/**
     * Получить всех авторов или одного
     *
     * @param int $authorId Идентификатор автора
     * @return array
     */
    public function getServices() 
    {
        $select = $this->getAdapter()->select()->from($this->_name);
        $select->order("id");
		$stmt = $this->getAdapter()->query($select);
        $dataArr = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);    
		$result = array();
		foreach ($dataArr as $k=>$v) {
			$result[] = new Services($v);
		}
        return $result;
    }

    public function getService($id) {
        $select = $this->getAdapter()->select()->from($this->_name);
        $select->where($this->_name.".id = ?", $id); 
        $stmt = $this->getAdapter()->query($select);
		$dataArr = $stmt->fetch(Zend_Db::FETCH_ASSOC);
		$this->setDataFromDBArray($dataArr);
        return $this;
    }
}